﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace BoringTodo.Extensions
{
    /// <summary>
    /// 提供文件操作的扩展方法
    /// </summary>
    public static class FileExtensions
    {
        /// <summary>
        /// 确保目录存在，如果不存在则创建
        /// </summary>
        /// <param name="path">目录路径</param>
        /// <returns>目录路径</returns>
        public static string EnsureDirectoryExists(this string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            return path;
        }

        /// <summary>
        /// 安全地将文本写入文件，确保目录存在
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="content">要写入的内容</param>
        /// <param name="append">是否追加模式</param>
        public static void SafeWriteAllText(this string path, string content, bool append = false)
        {
            string directory = Path.GetDirectoryName(path);
            if (!string.IsNullOrEmpty(directory))
            {
                directory.EnsureDirectoryExists();
            }
            
            if (append && File.Exists(path))
            {
                File.AppendAllText(path, content);
            }
            else
            {
                File.WriteAllText(path, content);
            }
        }

        /// <summary>
        /// 安全地将字节数组写入文件，确保目录存在
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="bytes">要写入的字节数组</param>
        public static void SafeWriteAllBytes(this string path, byte[] bytes)
        {
            string directory = Path.GetDirectoryName(path);
            if (!string.IsNullOrEmpty(directory))
            {
                directory.EnsureDirectoryExists();
            }
            
            File.WriteAllBytes(path, bytes);
        }

        /// <summary>
        /// 确保文件名有效，替换无效字符
        /// </summary>
        /// <param name="fileName">原始文件名</param>
        /// <returns>有效的文件名</returns>
        public static string MakeValidFileName(this string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
                return "unnamed";
                
            string invalidChars = new string(Path.GetInvalidFileNameChars());
            string invalidRegStr = string.Format(@"[{0}]", Regex.Escape(invalidChars));
            return Regex.Replace(fileName, invalidRegStr, "_");
        }

        /// <summary>
        /// 生成不与现有文件冲突的唯一文件名
        /// </summary>
        /// <param name="filePath">原始文件路径</param>
        /// <returns>唯一的文件路径</returns>
        public static string GetUniqueFilePath(this string filePath)
        {
            if (!File.Exists(filePath))
                return filePath;

            string folderPath = Path.GetDirectoryName(filePath);
            string fileName = Path.GetFileNameWithoutExtension(filePath);
            string extension = Path.GetExtension(filePath);
            
            int counter = 1;
            string newFilePath;
            
            do
            {
                newFilePath = Path.Combine(folderPath, $"{fileName}_{counter++}{extension}");
            } while (File.Exists(newFilePath));
            
            return newFilePath;
        }
    }
}
